Five programming problems every Software Engineer should be able to solve in less than 1 hour

http://www.shiftedup.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour

Problem 1

Write three functions that compute the sum of the numbers in a given list using a for-loop, a while-loop, and recursion.


In [ ]:
a = [1,2,3,4,5,6,7]

def adding(x):
    total = 0
    
    for i in x:
        total += i
    print(total)
    
    total = 0
    i = 0
    while i < len(x):
        total += x[i]
        i += 1
    print(total)
    
    return

adding(a)

In [ ]:
b = [1,2,3,4,5,6,7]

def recur(lst,counter,total):
    if counter == len(lst):
        print(total)
        return
    else:
        total += lst[counter]
        counter += 1
        return recur(lst,counter,total)

recur(b,0,0)

Problem 2

Write a function that combines two lists by alternatingly taking elements. For example: given the two lists [a, b, c] and [1, 2, 3], the function should return [a, 1, b, 2, c, 3].


In [ ]:
lst1=['a','b','c']
lst2=[1,2,3]

def combine(lst1,lst2):
    lst = []
    i = 0
    while i < len(lst1):
        lst.append(lst1[i])
        lst.append(lst2[i])
        i += 1
    lst
    print(lst)
    return

combine(lst1,lst2)

Problem 3

Write a function that computes the list of the first 100 Fibonacci numbers. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. As an example, here are the first 10 Fibonnaci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34.


In [ ]:
# fibonacci numbers 1-100
def fibonacci(i):
    k = 0
    num0 = 0
    num1 = 1

    print(num0)
    print(num1)
    
    while k < i:
        next_num = num0 + num1
        print(next_num)
        num0 = num1
        num1 = next_num
        k += 1
        
fibonacci(98)

In [ ]:
# fibonacci numbers 1-100
def fibonacci(i):
    fib = [0,1]
    k = 0
    
    while len(fib) < i:
        next_num = fib[k] + fib[k+1]
        fib.append(next_num)
        k+=1
    print(fib)
        
fibonacci(100)

Problem 4

Write a function that given a list of non negative integers, arranges them such that they form the largest possible number. For example, given [50, 2, 1, 9], the largest formed number is 95021.


In [ ]:
98750301291

In [7]:
#this will search for the whole list 1x for i_dwn
def list_sort(lst):
    iterations = 0
    
    while 1:
        i = 0
        j = i+1

        value_before = make_num(lst[:])

        while j < len(lst):       
            n0 = (int(str(lst[i]) + str(lst[j]))) 
            n1 = (int(str(lst[j]) + str(lst[i])))
            print('lst:',lst)
 
            if n0 < n1:
                lst[i],lst[j] = lst[j],lst[i]
            i+=1
            j=i+1
            iterations += 1

        value_after = make_num(lst[:])

        if value_before < value_after:
            continue            
        else:
            print('before:',value_before)
            print('after:',value_after)
            print('iterations:',iterations)
            break
    return


# function to print the list as a number
def make_num(lst_sorted):
    s_total = ''
    for i in lst_sorted:
        s_total += str(i)
    return int(s_total)


#lst = [5, 50, 56]
lst = [17, 32, 91, 7, 46]

# this calls the initial sort
list_sort(lst)


lst: [17, 32, 91, 7, 46]
lst: [32, 17, 91, 7, 46]
lst: [32, 91, 17, 7, 46]
lst: [32, 91, 7, 17, 46]
lst: [32, 91, 7, 46, 17]
lst: [91, 32, 7, 46, 17]
lst: [91, 7, 32, 46, 17]
lst: [91, 7, 46, 32, 17]
lst: [91, 7, 46, 32, 17]
lst: [91, 7, 46, 32, 17]
lst: [91, 7, 46, 32, 17]
lst: [91, 7, 46, 32, 17]
before: 917463217
after: 917463217
iterations: 12

Problem 5

Write a program that outputs all possibilities to put + or - or nothing between the numbers 1, 2, ..., 9 (in this order) such that the result is always 100. For example: 1 + 2 + 34 – 5 + 67 – 8 + 9 = 100.


In [ ]: